package edu.ccut.saturn.studio.wfg.xml.loader;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import edu.ccut.saturn.studio.wfg.model.Shape;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnHumanNode;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnName;

public class SaturnHumanNodeLoader extends NodeLoader{

	@Override
	public List<Shape> loadNodes(Element root) {
		NodeList humanNodes = root.getElementsByTagName(SaturnName.XML_NAME);
		List<Shape> humanList = new ArrayList<Shape>();
		for(int i = 0 ; i<humanNodes.getLength();i++){
			Node human = humanNodes.item(i);
			if(SaturnName.KEY_NODE_ATTRIBUTE_TYPENAMEHUMAN.equals(super.getNodeAttributeValue(human, SaturnName.XML_TYPE))){
				SaturnHumanNode saturnHuman = new SaturnHumanNode();
				// 基本信息
				saturnHuman.setName(super.getNodeElementValue(human, Shape.KEY_NODE_NAME));
				saturnHuman.setId(super.getNodeAttributeValue(human, Shape.KEY_ATTRIBUTE_ID));
				saturnHuman.setNextId(super.getNodeAttributeValue(human, Shape.KEY_ATTRIBUTE_SHAPE_NEXT_ID));
				saturnHuman.setLocation(super.getLocation(human));
				saturnHuman.setSize(super.getSize());
				saturnHuman.setTimeLimit(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_TIMELIMIT));
				saturnHuman.setPolymeMode(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_POLYMEMODE));
				saturnHuman.setDescription(super.getNodeElementValue(human, SaturnHumanNode.KEY_NODE_DESCRIPTION));
				// logicKey扩展逻辑
				saturnHuman.setLogicKeyList(super.getSaturnAttributeList(human, SaturnName.KEY_NODE_ELEMENT_LOGICKEY, SaturnName.KEY_NODE_NAME_KEY, SaturnName.METHOD_GETKEY));
				saturnHuman.setAfterRequest(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_AFTERREQUEST));
				// formData表单信息
				saturnHuman.setFormDataAttributeList(super.getSaturnAttributeList(human, SaturnName.KEY_NODE_ELEMENT_FORMDATA, SaturnName.KEY_NODE_NAME_DATA,SaturnName.METHOD_GETSATURNATTRIB));
				saturnHuman.setUrl(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_URL));
				// user参与者
				saturnHuman.getSaturnParticipator().setUserAttributeList(super.getSaturnAttributeList(human, SaturnName.KEY_NODE_ELEMENT_USERS, SaturnName.KEY_NODE_NAME_USER, SaturnName.METHOD_GETUSER));
				saturnHuman.getSaturnParticipator().setObtainFromActivityRadioButton(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_OBTAINFROMACTIVITYRADIOBUTTON));
				saturnHuman.getSaturnParticipator().setValidateUserLogic(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_VALIDATEUSERLOGIC));
				saturnHuman.getSaturnParticipator().setUserFrom(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_USERFROM));
				saturnHuman.getSaturnParticipator().setCustomDefineParticipant(super.getCustomDefineParticipant(human, SaturnName.KEY_NODE_ELEMENT_CUSTOMDEFINEPARTICIPANT, SaturnName.KEY_NODE_NAME_KEY, SaturnName.METHOD_GETKEY));
				
				// 没人要的
//				saturnHuman.setStartPolicy(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_STARTPOLICY));
//				saturnHuman.setOffsetMode(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_OFFSETMODE));
//				saturnHuman.setWorkItemNumStrategy(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_WORKITEMNUMSTRATEGY));
				// 多工作项
				saturnHuman.getSaturnMultiWork().setIsMultiWorkItem(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_ISMULTIWORKITEM));
				saturnHuman.getSaturnMultiWork().setIsFinishAll(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_ISFINISHALL));
				saturnHuman.getSaturnMultiWork().setFinishRequiredNum(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_FINISHREQUIREDNUM));
				saturnHuman.getSaturnMultiWork().setFinishRequiredPercent(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_FINISHREQUIREDPERCENT));
				saturnHuman.getSaturnMultiWork().setIsAutoCancel(super.getNodeElementValue(human, SaturnName.KEY_NODE_ELEMENT_ISAUTOCANCEL));
				
				humanList.add(saturnHuman);
			}
		}
		return humanList;
	}
}
